﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using BLL.EarningService;
using BLL;
using BLL.BaseDictionaryService;

namespace FamilyManager
{
    public partial class EarningRecordsInputForm : Form
    {
        private EarningRecordsInputForm()
        {
            InitializeComponent();
        }

        private static EarningRecordsInputForm instance;

        public static EarningRecordsInputForm CreateInstance()
        {
            if (instance == null)
            {
                instance = new EarningRecordsInputForm();
            }
            return instance;
        }

        private void BindcbUsers()
        {
            QueryUsersTransaction command = new QueryUsersTransaction();
            command.Execute();
            cbUsers.DataSource = command.Users;
            cbUsers.DisplayMember = "Db_NickName";
            cbUsers.ValueMember = "Db_OID";
        }

        private void BindcbEarningCategory()
        {
            QueryEarningCategoryTransaction objQueryEarningCategoryTransaction = new QueryEarningCategoryTransaction();
            objQueryEarningCategoryTransaction.Execute();
            cbEarningCategory.DataSource = objQueryEarningCategoryTransaction.ResultList;
            cbEarningCategory.DisplayMember = "db_EarningCategory";
            cbEarningCategory.ValueMember = "Db_OID";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                T_EarningRecordsObject objT_EarningRecordsObject = new T_EarningRecordsObject();
                objT_EarningRecordsObject.Db_EarningDate = DateTime.Parse(dtpEarningDate.Value.ToShortDateString());
                objT_EarningRecordsObject.Db_EarningSum = decimal.Parse(txtSum.Text);
                objT_EarningRecordsObject.Db_EarningCategory = (int)cbEarningCategory.SelectedValue;
                objT_EarningRecordsObject.Db_EarningObject = (int)cbUsers.SelectedValue;
                objT_EarningRecordsObject.Db_EarningMemo = txtMemo.Text;

                AddEarningRecordTransaction objAddEarningRecordTransaction = new AddEarningRecordTransaction(objT_EarningRecordsObject);
                objAddEarningRecordTransaction.Execute();
                if (objAddEarningRecordTransaction.OpState)
                {
                    //成功
                    BingvEarningList();
                }
                else
                {
                    //失败
                }
            }
        }

        private bool CheckInput()
        {
            if (txtSum.Text.Length == 0)
            {
                MessageBox.Show("请输入收入金额", "温馨提示");
                txtSum.Focus();
                return false;
            }
            decimal tmpd;
            if (!decimal.TryParse(txtSum.Text, out tmpd))
            {
                MessageBox.Show("请正确输入收入金额", "温馨提示");
                txtSum.Focus();
                return false;
            }
            return true;
        }

        private void EarningRecordsInputForm_Load(object sender, EventArgs e)
        {
            txtSum.Focus();
            txtSum.SelectAll();
            BindcbUsers();
            BindcbEarningCategory();

            BingvEarningList();
        }

        private void txtSum_Enter(object sender, EventArgs e)
        {
            txtSum.Clear();
        }

        private void BingvEarningList()
        {
            gvEarningList.AutoGenerateColumns = false;
            gvEarningList.Columns.Clear();

            DateTime beginDate = new DateTime(dtpEarningDate.Value.Year, dtpEarningDate.Value.Month, 1);
            DateTime endDate = new DateTime(dtpEarningDate.Value.Year, dtpEarningDate.Value.Month, ToolBox.GetEndDay(dtpEarningDate.Value.Year,dtpEarningDate.Value.Month));
            QueryEarningRecordByTimeTransaction objQueryEarningRecordByTimeTransaction = new QueryEarningRecordByTimeTransaction(beginDate, endDate);
            IList<T_EarningRecordsObject> list = objQueryEarningRecordByTimeTransaction.Query();
            gvEarningList.DataSource = list;

            DataGridViewTextBoxColumn dcDate = new DataGridViewTextBoxColumn();
            dcDate.DataPropertyName = "Db_EarningDate";
            dcDate.HeaderText = "收入日期";
            gvEarningList.Columns.Add(dcDate);

            DataGridViewTextBoxColumn dcSum = new DataGridViewTextBoxColumn();
            dcSum.DataPropertyName = "Db_EarningSum";
            dcSum.HeaderText = "收入金额";
            gvEarningList.Columns.Add(dcSum);

            DataGridViewTextBoxColumn dcCategory = new DataGridViewTextBoxColumn();
            dcCategory.DataPropertyName = "EarningCategory";
            dcCategory.HeaderText = "收入类别";
            gvEarningList.Columns.Add(dcCategory);

            DataGridViewTextBoxColumn dcUser = new DataGridViewTextBoxColumn();
            dcUser.DataPropertyName = "EarningUser";
            dcUser.HeaderText = "收入人";
            gvEarningList.Columns.Add(dcUser);

            DataGridViewTextBoxColumn dcMemo = new DataGridViewTextBoxColumn();
            dcMemo.DataPropertyName = "Db_EarningMemo";
            dcMemo.HeaderText = "备注";
            gvEarningList.Columns.Add(dcMemo);
        }
    }
}
